package com.cloudminds.resttest.service;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
/**
 * @Author
 * @Description //TODO $
 * @Date $ $
 **/

public class WebSocketTest {

        //private String wsServerUrl;

        //静态变量，用来记录当前在线连接数。应该把它设计成线程安全的。
        private static String seq ;
        //concurrent包的线程安全Set，用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话，可以使用Map来存放，其中Key可以为用户标识
        private static CopyOnWriteArraySet<WebSocketTest> webSocketSet = new CopyOnWriteArraySet<WebSocketTest>();
        private static ConcurrentHashMap<String, String> hashMap = new ConcurrentHashMap<>();

        public static void connect() throws URISyntaxException {

        URI url = new URI("ws://172.16.13.109:9443/hari");

        WebSocketClient wsServerUrl1 = new WebSocketClient(url) {
         @Override
         public void onOpen(ServerHandshake handshakedata) {
            //存起来
             hashMap.put(seq=UUID.randomUUID().toString().replaceAll("-", ""),url.toString());
            //
            String  s =  hashMap.get(seq);


            System.out.print("s======="+s+"seq========="+seq);

         }

         @Override
         public void onMessage(String message) {

             for (int i=0; i<1; i++){
            // seq 与返回的一致 就返回上一层


             }
             System.out.print("ddddd");
         }

         @Override
         public void onClose(int code, String reason, boolean remote) {

         }

         @Override
         public void onError(Exception ex) {

         }
     };

            wsServerUrl1.connect();
            System.out.print("connect---------");
        }

}
